// app.js
App({
  onLaunch(options) {
    // 登录
    console.log(options)
    var referrerId=options.query?options.query.referrerId||'':''
    if(options.query&&options.query.scene){
      var scene = decodeURIComponent(options.query.scene);
     
      var arrPara = scene.split("&");
      console.log(arrPara)
      var arr = [];
      for (var i in arrPara) {
          arr = arrPara[i].split("=");
          if(arr[0]=='referrerId'){
            referrerId=arr[1]||''
          }
          
      }
    }
  //   if (options.scene) {
  //     var scene = decodeURIComponent(options.scene).query;
  //     console.log("scene is ", scene);
  //     // 可以进一步根据参数的格式进行拆分和处理
  //     var arrPara = scene.split("&");
  //     var arr = [];
  //     for (var i in arrPara) {
  //         arr = arrPara[i].split("=");
  //         referrerId=arr[1]||''
  //         // wx.setStorageSync(arr[0], arr[1]);
  //         console.log(arr[0], "=", arr[1]);
  //     }
  // }


    this.getLogin(referrerId)
    var that=this
    this.getIsIphoneX()
    this.getBarHeight()
    
  },
  //获取顶部高度
  getBarHeight(){
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: res => {
        let statusBarHeight = res.statusBarHeight,
          navHeight =  menuButtonObject.height + (menuButtonObject.top - statusBarHeight)*2;//导航高度
       
          this.globalData.statusBarHeight = statusBarHeight;
          this.globalData.navHeight = navHeight;
          this.globalData.headerHeight=statusBarHeight+navHeight
      },
      fail(err) {
        console.log(err);
      }
    })
  },
  //登录
  getLogin(referrerId){
    var that=this
    wx.login({
      success: (res) => {
        if (res.code) {
          console.log(res)
          var params={
            auth_code:res.code
          }
          wx.request({
            url: this.globalData.host+'/wxapp-login', //仅为示例，并非真实的接口地址
            data: {
              code:res.code,
              referrerId:referrerId
            },
            method:'POST',
            header: {
              'content-type': 'application/json' // 默认值
            },
            success (res) {
              if(res.data.code==200){          
                if(res.data.data.phone){
                  that.globalData.isPhone=true
                }else{
                  that.globalData.isPhone=false
                }      
                  that.globalData.userInfo=res.data.data
                  that.globalData.token=res.data.data.token
                  wx.setStorageSync('userInfo',res.data.data)
                  wx.setStorageSync('token',res.data.data.token)
                  
               
             }else{
              wx.showToast({
                title:res.message,
                icon:'none',
                duration:3000
               });
             }
            }
          })
        }
      },
    });
  },
  getIsIphoneX(){
    var that=this
    var isIphoneX=false
    wx.getSystemInfo({
      success:function(res){
        console.log(res)
        if(res.model.indexOf('iPhone')!=-1||res.model.indexOf('iphone')!=-1){
          if (['iPhone 5','iPhone5,1','iPhone5,2','iPhone 6','iPhone6,1','iPhone6,2','iPhone 7','iPhone7,1','iPhone7,2','iPhone 8','iPhone10,1','iPhone 6 Plus','iPhone 7 Plus','iPhone 8 Plus','iPhone 6/7/8','iPhone 6/7/8 Plus'].includes(res.model)) {
            isIphoneX =false;
          } else {
            isIphoneX=true;
          }
        }else{
          isIphoneX=false;
        }
        console.log('isIphoneX',isIphoneX)
        that.globalData.isIphoneX=isIphoneX
      }
    });
  },
  editTabBar: function () {    
    var tabBar= this.globalData.tabbar;//获取tabbar的数据赋值给tabBar
    var pages = getCurrentPages();//获取当前页面栈的实例，以数组形式按栈的顺序给出
    var currentPage = pages[pages.length - 1];
    var url = '/' + currentPage .route;//获取路径
    for (var i = 0; i < tabBar.items.length; i++) {
      tabBar.items[i].active = false;//令所有的底部导航都是正常状态
      if (tabBar.items[i].pagePath == url) {//若是点击的路径
        tabBar.items[i].active = true;//根据页面地址设置当前页面状态
      }
    }
    //设置数据
    currentPage.setData({
       tabbar:tabBar
    });
  },
  globalData: {
    host:'https://r-api.cqhuoshiji.com',
    isPhone:true,
    isIphoneX:'',
    statusBarHeight:20,//导航高度
    navHeight:40,//胶囊按钮与顶部的距离
    headerHeight:60,
    userInfo: null,
    homeNumber:1,//首页弹窗广告次数
    tabbar:{
      textColor: "#262626",
      selectedColor: "#C2131C",
      backgroundColor: "#fff",
      borderStyle: "#d5d5d5",
      items: [        
        {
          pagePath: "/pages/home/index/index",
          name: "首页",
          type:'index',
          icon: "/images/tabar/home_line.png",
          activeIcon: "/images/tabar/home_fill.png"
        },
        {
          pagePath: "/pages/classify/index/index",
          name: "分类",
          type:'classify',
          icon: "/images/tabar/grid_line.png",
          activeIcon: "/images/tabar/grid_fill.png"
        },
        {
          pagePath: "/pages/shopCart/index/index",
          name: "购物车",
          type:'shopCart',
          icon: "/images/tabar/shop_cart_line.png",
          activeIcon: "/images/tabar/shop_cart_fill.png"
        },
        {
          pagePath: "/pages/user/index/index",
          name: "我的",
          type:'user',
          icon: "/images/tabar/user_line.png",
          activeIcon: "/images/tabar/user_fill.png"
        }     
      ],
      position: "bottom"
    },
  }
})
